package com.wl.java_spring_boot01.modules.test.controller;

import com.wl.java_spring_boot01.modules.common.vo.Result;
import com.wl.java_spring_boot01.modules.common.vo.SearchVo;
import com.wl.java_spring_boot01.modules.test.entity.Student;
import com.wl.java_spring_boot01.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 汪林
 * @date 2020/8/13 8:42
 */
@RestController
@RequestMapping(value = "/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**增加
     * 127.0.0.1/api/student ---- post
     * {"studentName":"zt","studentCard":{"cardId":"1"}}
     */
    @RequestMapping(value = "/student",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Student> insertStudent(@RequestBody Student student){
        return studentService.insertStudent(student);
    }

    /**
     * --id查找
     * 127.0.0.1/api/student/1 ---- get
     */
    @RequestMapping(value = "/student/{studentId}")
    public Result<Student> getStudentByStudentId(@PathVariable Integer studentId){
        return studentService.findStudentById(studentId);
    }
    /**
     * --分页查询(根据example,pageable)
     * 127.0.0.1/api/students ---- post
     * findAll(example,pageable)
     * {"currentPage":"1","pageSize":"5","keyWord":"z","orderBy":"studentName","sort":"desc"}
     * {"currentPage":"1","pageSize":"5","sort":"desc"}--findAll(pageable)
     */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page<Student> getStudentsBySearchVo(@RequestBody SearchVo searchVo){
        return studentService.getStudentsBySearchVo(searchVo);
    }

    /**
     * --分页查询(根据sort)
     * 127.0.0.1/api/students ---- get
     */
    @GetMapping("/students")
    public List<Student> getStudents() {
        return studentService.getStudents();
    }

    /**
     * 127.0.0.1/api/students?studentName=hujiang ---- get
     */
    @GetMapping("/studentss")
    public List<Student> getStudentsByParams(
            @RequestParam String studentName,
            @RequestParam(required = false, defaultValue = "0") Integer cardId) {
        return studentService.getStudentsByStudentName(studentName, cardId);
    }
}
